
$NG.AllReady(function (page, { useAction, useDataIndexChange, useBeforeClick, useValuesChange, useClick, useUpdateRow, subscribe, useUpdateRows }) {
    console.log('detail Ready');
    var mstform = $NG.getCmpApi('p_form_xmzyfzr_m');
    var grid = $NG.getCmpApi('p_form_xmzyfzr_d1');
    console.log(mstform);
    console.log(mstform.getItem('u_bdmc').getValue());
    if (page.oType == "add") {
        const defaultRows = [
            { u_nrzw: "项目经理" },
            { u_nrzw: "项目总工" },
            { u_nrzw: "安全总监" },
            { u_nrzw: "质量总监" }
        ];

        // 添加到明细表
        grid.addRows(defaultRows);
    }
    useDataIndexChange(({ args, instance }) => {
        const record = args[0];
        console.log(record);
        $NG.execServer("searchHr2", { phid: record.u_xm }, (res2) => {
            if (res2.count === 0) return;
            const data = JSON.parse(res2.data)[0].extendObjects;
            console.log('searchHr2', data);
            console.log(data.mobile1);

            record.u_lxfs = data.mobile1; //赋值
            record.u_szdwbm = data.phid_dept; //赋值
            record.u_szdwbm_EXName = data.oname; //赋值
            grid.refreshView();
        });
        // record.u_sysl = record.u_sl; //赋值
        // grid.updateRow(record);
        grid.refreshView();
    }, "u_xm");
    if (mstform.getItem('u_ywlb').getValue() == 6870000000000112) {
        $NG.updateUI((updater) => {
            const fields = ["u_hylb", 'u_ejhylb', 'u_scfw'];
            fields.forEach((field) => {
                updater.fieldSetForm.p_form_xmzyfzr_m[field].setProps({
                    hidden: true, //隐藏
                    required: false, //必输
                    // disabled: true, //只读
                });
            });
        });
    }
    if (mstform.getItem('u_sccyllb').getValue() == 6870000000000085) {
        $NG.updateUI((updater) => {
            const fields = ["u_hylb", 'u_ejhylb', 'u_scfw'];
            fields.forEach((field) => {
                updater.fieldSetForm.p_form_xmzyfzr_m[field].setProps({
                    hidden: true, //隐藏
                    required: false, //必输
                    // disabled: true, //只读
                });
            });
        });
    }
    useValuesChange(({ args }) => {
        let user_bdmc = mstform.getItem('u_bdmc').getValue();
        console.log("当前BDMC值:", user_bdmc);
        $NG.execServer("searchXmxx", { u_bdmc: user_bdmc }, (res2) => {
            if (res2.count === 0) return;
            const data = JSON.parse(res2.data)[0].extendObjects;
            console.log('时间', data);
            $NG.updateState((updater) => {
                updater.data.p_form_xmzyfzr_m.setProps({
                    u_tbsj: data.u_tbsj,

                });
            });


        });
        $NG.execServer("searchBdmc2", { user_bdmc: user_bdmc }, (res) => {
            if (res.count === 0) return;

            const bdmcData = JSON.parse(res.data)[0].extendObjects;
            console.log("searchBdmc2返回:", bdmcData);
            if (bdmcData.user_yelb == 6870000000000112) {
                $NG.updateUI((updater) => {
                    const fields = ["u_hylb", 'u_ejhylb', 'u_scfw'];
                    fields.forEach((field) => {
                        updater.fieldSetForm.p_form_xmzyfzr_m[field].setProps({
                            hidden: true, //隐藏
                            required: false, //必输
                            // disabled: true, //只读
                        });
                    });
                });
            }
            if (bdmcData.user_sccyllb == 6870000000000085) {
                $NG.updateUI((updater) => {
                    const fields = ["u_hylb", 'u_ejhylb', 'u_scfw'];
                    fields.forEach((field) => {
                        updater.fieldSetForm.p_form_xmzyfzr_m[field].setProps({
                            hidden: true, //隐藏
                            required: false, //必输
                            // disabled: true, //只读
                        });
                    });
                });
            }
            $NG.execServer("searchXmms", { phid: bdmcData.user_xmms }, (res2) => {
                if (res2.count === 0) return;
                const xmmsData = JSON.parse(res2.data)[0].extendObjects;
                const user_xmms_EXName = xmmsData.c_name;
                $NG.updateState((updater) => {
                    updater.data.p_form_xmzyfzr_m.setProps({
                        u_xmbh: bdmcData.bill_no,
                        u_yzdw: bdmcData.user_yzdw,
                        u_tbsj: bdmcData.user_tbsj,
                        bill_name: bdmcData.project_name,
                        // u_tbsj: bdmcData.project_name,
                        u_scfw: bdmcData.user_scfw,
                        u_xmms: {
                            value: bdmcData.user_xmms,
                            label: user_xmms_EXName,
                        }
                    });
                });
            });
            $NG.execServer("searchYwlb", { phid: bdmcData.user_yelb }, (res2) => {
                if (res2.count === 0) return;
                const xmmsData = JSON.parse(res2.data)[0].extendObjects;
                const user_xmms_EXName = xmmsData.c_name;
                $NG.updateState((updater) => {
                    updater.data.p_form_xmzyfzr_m.setProps({
                        u_ywlb: {
                            value: bdmcData.user_yelb,
                            label: user_xmms_EXName,
                        }
                    });
                });
            });
            $NG.execServer("searchSccyllb", { phid: bdmcData.user_sccyllb }, (res2) => {
                if (res2.count === 0) return;
                const xmmsData = JSON.parse(res2.data)[0].extendObjects;
                const user_xmms_EXName = xmmsData.c_name;
                $NG.updateState((updater) => {
                    updater.data.p_form_xmzyfzr_m.setProps({
                        u_sccyllb: {
                            value: bdmcData.user_sccyllb,
                            label: user_xmms_EXName,
                        }
                    });
                });
            });
            $NG.execServer("searchYjhylb", { phid: bdmcData.user_hylb }, (res2) => {
                if (res2.count === 0) return;
                const xmmsData = JSON.parse(res2.data)[0].extendObjects;
                const user_xmms_EXName = xmmsData.c_name;
                $NG.updateState((updater) => {
                    updater.data.p_form_xmzyfzr_m.setProps({
                        u_hylb: {
                            value: bdmcData.user_hylb,
                            label: user_xmms_EXName,
                        }
                    });
                });
            });
            $NG.execServer("searchYwlb", { phid: bdmcData.user_ejhylb }, (res2) => {
                if (res2.count === 0) return;
                const xmmsData = JSON.parse(res2.data)[0].extendObjects;
                const user_xmms_EXName = xmmsData.c_name;
                $NG.updateState((updater) => {
                    updater.data.p_form_xmzyfzr_m.setProps({
                        u_ejhylb: {
                            value: bdmcData.user_ejhylb,
                            label: user_xmms_EXName,
                        }
                    });
                });
            });
            $NG.execServer("serachTbdw", { phid: bdmcData.user_tbdw }, (res2) => {
                if (res2.count === 0) return;
                const xmmsData = JSON.parse(res2.data)[0].extendObjects;
                const user_xmms_EXName = xmmsData.oname;
                $NG.updateState((updater) => {
                    updater.data.p_form_xmzyfzr_m.setProps({
                        u_tbdw: {
                            value: bdmcData.user_tbdw,
                            label: user_xmms_EXName,
                        }
                    });
                });
            });


        });
    }, "p_form_xmzyfzr_m.u_bdmc");
    useValuesChange(({ args }) => {
        let u_ywlb = mstform.getItem('u_ywlb').getValue();
        console.log("当前u_ywlb值:", u_ywlb);
        if (u_ywlb == 6870000000000112) {
            $NG.updateUI((updater) => {
                const fields = ["u_hylb", 'u_ejhylb', 'u_scfw'];
                fields.forEach((field) => {
                    updater.fieldSetForm.p_form_xmzyfzr_m[field].setProps({
                        hidden: true, //隐藏
                        required: false, //必输
                        // disabled: true, //只读
                    });
                });
            });
        } else {
            $NG.updateUI((updater) => {
                const fields = ["u_hylb", 'u_ejhylb', 'u_scfw'];
                fields.forEach((field) => {
                    updater.fieldSetForm.p_form_xmzyfzr_m[field].setProps({
                        hidden: false, //隐藏
                        required: true, //必输
                        // disabled: true, //只读
                    });
                });
            });
        }

    }, "p_form_xmzyfzr_m.u_ywlb");
    useValuesChange(({ args }) => {
        let u_sccyllb = mstform.getItem('u_sccyllb').getValue();
        console.log("当前u_sccyllb值:", u_sccyllb);
        if (u_sccyllb == 6870000000000085) {
            $NG.updateUI((updater) => {
                const fields = ["u_hylb", 'u_ejhylb', 'u_scfw'];
                fields.forEach((field) => {
                    updater.fieldSetForm.p_form_xmzyfzr_m[field].setProps({
                        hidden: true, //隐藏
                        required: false, //必输
                        // disabled: true, //只读
                    });
                });
            });
        } else {
            $NG.updateUI((updater) => {
                const fields = ["u_hylb", 'u_ejhylb', 'u_scfw'];
                fields.forEach((field) => {
                    updater.fieldSetForm.p_form_xmzyfzr_m[field].setProps({
                        hidden: false, //隐藏
                        required: true, //必输
                        // disabled: true, //只读
                    });
                });
            });
        }

    }, "p_form_xmzyfzr_m.u_sccyllb");

    useUpdateRows(({ args, table }) => {
        console.log($NG.getCmpApi('p_form_xmzyfzr_d1').getRows());
        console.log(grid.getAllData());

    }, 'p_form_xmzyfzr_d1');
    useBeforeClick(async function ({ args }) {
        const detailApi = $NG.getCmpApi('p_form_xmzyfzr_d1');
        const allRows = detailApi.getRows();
        console.log(allRows);

        const invalidRows = [];
        for (let i = 0; i < allRows.length; i++) {
            const row = allRows[i];
            const rowNumber = i + 1;
            if (!row.hasOwnProperty('u_xmzgzz')) {
                invalidRows.push(rowNumber);
                continue;
            }
            const value = row.u_xmzgzz;
            if (value === null || value === undefined || value.trim() === '') {
                invalidRows.push(rowNumber);
            }
        }
        console.log(invalidRows);

        if (invalidRows.length > 0) {
            $NG.alert(`以下行的[附件]未上传：${invalidRows.join(', ')}`)
            try {
                invalidRows.forEach(rowIndex => {
                    detailApi.setRowStatus(rowIndex - 1, "error");
                });
            } catch (e) {
                console.warn("无法高亮行:", e);
            }
            return false;
        }
    }, "save");
}, function () {
    console.log('list Ready');
});

